package com.pig4cloud.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.mall.product.dto.CategoryParam;
import com.pig4cloud.mall.product.entity.ProductCategory;
import com.pig4cloud.mall.product.entity.ProductCategoryAttributeRelation;
import com.pig4cloud.mall.product.mapper.ProductCategoryAttributeRelationMapper;
import com.pig4cloud.mall.product.mapper.ProductCategoryMapper;
import com.pig4cloud.mall.product.service.ProductCategoryService;
import com.pig4cloud.mall.product.vo.CategoryVo;
import com.pig4cloud.pig.common.core.util.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 产品分类
 *
 * @author fangyu
 * @date 2020-12-26 23:41:08
 */
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements ProductCategoryService {

	@Resource
	private ProductCategoryMapper categoryMapper;

	@Resource
	private ProductCategoryAttributeRelationMapper productCategoryAttributeRelationMapper;

	@Override
	public int create(CategoryParam categoryParam) {
		ProductCategory productCategory = new ProductCategory();
		productCategory.setProductCount(0);
		BeanUtils.copyProperties(categoryParam, productCategory);
		// 没有父分类时为一级分类
		setCategoryLevel(productCategory);
		int count = categoryMapper.insert(productCategory);
		// 创建筛选属性关联
		List<Long> productAttributeIdList = categoryParam.getProductAttributeIdList();
		if (!CollectionUtils.isEmpty(productAttributeIdList)) {
			insertRelationList(productCategory.getId(), productAttributeIdList);
		}
		return count;
	}

	@Override
	public List<CategoryVo> listCategory(Long parentId) {
		QueryWrapper<ProductCategory> wrapper = new QueryWrapper<>();
		if (parentId == null) {
			parentId = 0L;
		}
		if (parentId != 0) {
			wrapper.eq("parent_id", parentId);
			wrapper.orderByDesc("sort");
		} else {
			wrapper.eq("parent_id", 0);
		}
		List<ProductCategory> productCategories = categoryMapper.selectList(wrapper);
		List<CategoryVo> categoryVoList = new ArrayList<>();
		productCategories.forEach(productCategory -> {
			CategoryVo categoryVo = new CategoryVo();
			BeanUtils.copyProperties(productCategory, categoryVo);
			Integer count = categoryMapper.selectCount(new QueryWrapper<ProductCategory>().eq("parent_id", productCategory.getId()));
			categoryVo.setHasChildren(count > 0);
			categoryVoList.add(categoryVo);
		});
		return categoryVoList;
	}

	@Override
	public PageResult pageCategory(Long parentId, Integer page, Integer pageSize) {
		IPage<ProductCategory> iPage;
		QueryWrapper<ProductCategory> wrapper = new QueryWrapper<>();
		if (parentId != 0) {
			wrapper.eq("parent_id", parentId);
			wrapper.orderByDesc("sort");
		} else {
			wrapper.eq("parent_id", 0);
		}
		iPage = categoryMapper.selectPage(new Page<>(page, pageSize), wrapper);
		return PageResult.buildPage(iPage);
	}

	@Override
	public int delete(Long id) {
		return 0;
	}

	@Override
	public int update(Long id, CategoryParam categoryParam) {
		return 0;
	}

	/**
	 * 批量插入商品分类与筛选属性关系表
	 *
	 * @param productCategoryId      商品分类id
	 * @param productAttributeIdList 相关商品筛选属性id集合
	 */
	private void insertRelationList(Long productCategoryId, List<Long> productAttributeIdList) {
		List<ProductCategoryAttributeRelation> relationList = new ArrayList<>();
		for (Long productAttrId : productAttributeIdList) {
			ProductCategoryAttributeRelation relation = new ProductCategoryAttributeRelation();
			relation.setProductAttributeId(productAttrId);
			relation.setProductCategoryId(productCategoryId);
			relationList.add(relation);
		}
		productCategoryAttributeRelationMapper.insertList(relationList);
	}


	/**
	 * 根据分类的parentId设置分类的level
	 */
	private void setCategoryLevel(ProductCategory productCategory) {
		//没有父分类时为一级分类
		if (productCategory.getParentId() == 0) {
			productCategory.setLevel(0);
		} else {
			//有父分类时选择根据父分类level设置
			ProductCategory parentCategory = categoryMapper.selectById(productCategory.getParentId());
			if (parentCategory != null) {
				productCategory.setLevel(parentCategory.getLevel() + 1);
			} else {
				productCategory.setLevel(0);
			}
		}
	}
}
